package com.hzhh123.app05.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.hzhh123.app05.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListItemAdapter extends BaseAdapter {
    private List<ListItem>mList;
    private Context mContext;
    LayoutInflater inflater;
    public static final Map<Integer, String> map = new HashMap<Integer, String>();

    public ListItemAdapter(List<ListItem> list, Context context) {
        mList = list;
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(view == null){
            view = inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.rg = (RadioGroup) view.findViewById(R.id.radioGroup1);
            holder.rb1=(RadioButton)view.findViewById(R.id.checkbox1);
            holder.rb2=(RadioButton)view.findViewById(R.id.checkbox2);
            holder.rb3=(RadioButton)view.findViewById(R.id.checkbox3);
            holder.rb4=(RadioButton)view.findViewById(R.id.checkbox4);
            holder.tv1 = (TextView) view.findViewById(R.id.t1);
            holder.tv2 = (TextView) view.findViewById(R.id.t2);
            view.setTag(holder);

        }else{
            holder = (ViewHolder) view.getTag();
        }
        //①取消监听，清除选中状态，解决滚动出现选择混乱问题
        holder.rg.setOnCheckedChangeListener(null);
        holder.rg.clearCheck();
        ListItem listItem = mList.get(i);
        if(listItem.getStatus()!=null && listItem.getStatus().equals("1")){
            holder.rb1.setChecked(true);
        }
        if(listItem.getStatus()!=null && listItem.getStatus().equals("2")){
            holder.rb2.setChecked(true);
        }
        if(listItem.getStatus()!=null && listItem.getStatus().equals("0")){
            holder.rb3.setChecked(true);
        }

        if(listItem.getStatus()==null || listItem.getStatus().equals("")){
            holder.rb4.setChecked(true);
        }

        holder.tv1.setText(mList.get(i).getId());
        holder.tv2.setText(mList.get(i).getName());
        RadioGroup rgBtn = holder.rg;
        rgBtn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rbtn = (RadioButton) group.findViewById(checkedId);
                //设置选中值保存在map里
                map.put(i, rbtn.getText().toString());
                //② 结合取消监听①，重新设置选中值
                switch (checkedId){
                    case R.id.checkbox1:
                        mList.get(i).setStatus("1");
                        break;
                    case R.id.checkbox2:
                        mList.get(i).setStatus("2");
                        break;
                    case R.id.checkbox3:
                        mList.get(i).setStatus("0");
                        break;
                    case R.id.checkbox4:
                        mList.get(i).setStatus(null);
                        break;
                }

               // Toast.makeText(mContext, rbtn.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

    public class ViewHolder{
        RadioGroup rg;
        RadioButton rb1;
        RadioButton rb2;
        RadioButton rb3;
        RadioButton rb4;
        TextView tv1;
        TextView tv2;
    }
}
